package com.imooc.jdbc.hrapp.command;

import com.imooc.jdbc.common.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class UpdateCommand implements Command{


    @Override
    public void execute() {
        Scanner in = new Scanner(System.in);

//        System.out.println("请输入员工部门：");
//        String depName = in.next();

        System.out.println("请输入员工工号：");
        String empNo = in.next();

        System.out.println("请输入员工调薪薪资：");
        float salary = in.nextFloat();

        Connection conn = null;
        PreparedStatement preStmt = null;

        int cnt = 0;
        try {
            conn = DbUtils.getConnection();
            String updateSql = "update employee  set salary = ? where id=?";
            preStmt = conn.prepareStatement(updateSql);

            preStmt.setFloat(1, salary);
            preStmt.setString(2, empNo);

            cnt = preStmt.executeUpdate();

            System.out.println("cnt:" + cnt);

            if(cnt == 1){
                System.out.println("员工调薪已办理");
            }else{
                System.out.println("未找到该员工");
            }



        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeConnection(null, preStmt, conn);
        }

    }
}
